<template>
  <div class="home">
    <div class="home-container">
      <h3>任务列表</h3>
    </div>
    <div class="home-body">
      <h4>
        添加任务:<input
          placeholder="请输入任务"
          v-model="todo"
          @keyup.enter="enter"
        />
      </h4>
    </div>
    <div class="home-foot">
      <div class="home-left">
        <ul>
          <!--循环任务列表-->
          <li
            v-for="obj in nav"
            :key="obj.title"
            :class="isclick == obj.flag ? 'chosStyle' : 'notChosStyle'"
            @click="changeNav(obj.flag)"
            :style="{
              backgroundColor: isclick == obj.flag ? 'rgb(166, 186, 247)' : '#fff',
            }"
          >
            {{ obj.title }}<span>({{ getLength(obj.flag) }})</span>
          </li>
        </ul>
      </div>
      <div class="home-right">
        <!--任务-->
          <p  v-if="showList.length==0" :style="{textAlign:'center',marginTop:'50px'}">当前还没有添加任何任务哦!</p>
        <div class="home-list" v-for="(item, index) in showList" :key="index">
          <div @click="changeState(item)" class="yes">
            <p v-if="item.icom" >√</p>
          </div>
          <div
            :style="{
              color: item.icom ? '#a7a5a5' : '#666666',
              textDecoration: item.icom ? 'line-through' : 'none',
            }"
          >
            {{ item.title }}
          </div>
          <div class="box" @click="deleteList(item)">x</div>
        </div>
      </div>
      <router-view />
    </div>
  </div>
</template>
<script>
var store = {
  get(name){
    return JSON.parse( localStorage.getItem(name) )
  },
  set(name,list){
    localStorage.setItem(name,JSON.stringify(list))
  }
}
export default {
  name: "home",
  data() {
    return {
      todo: "",
      isclick: "not",//默认列表
      nav: [
        { title: "未完成任务", flag: "not" },
        { title: "已完成任务", flag: "is" },
        { title: "全部任务", flag: "all" },
      ],
      list: store.get('todolist'),
    };
  },
  computed: {
    showList() {
      let arr = [];
      switch (this.isclick) {
        case "not":
          arr = this.list.filter((item) => {
            return !item.icom;
          });
          break;
        case "is":
          arr = this.list.filter((item) => {
            return item.icom;
          });
          break;
        case "all":
          arr = this.list;
          break;
      }
      return arr;
    },
  },
  watch:{
    list:{
      hander(newV,oldV){
        store.set(this.list);
        console.log(newV);
        console.log(oldV);
      },
      deep:true
    }
  },
    created() {},
  methods: {
    enter() {
      console.log("===>", this.todo);
      this.list.unshift({
        title: this.todo,
        icom: false,
      });
      this.todo = "";
      store.set('todolist',this.list)
    },
    changeNav(flag) {
      console.log("当前", flag);
      this.isclick = flag;
    },
    getLength(flag){
      console.log(flag);
      let arr = [];
      switch (flag) {
        case "not":
          arr = this.list.filter((item) => {
            return !item.icom;
          });
          break;
        case "is":
          arr = this.list.filter((item) => {
            return item.icom;
          });
          break;
        case "all":
          arr = this.list;
          break;
      }
      return arr.length;
    },
    changeState(item){
      item.icom = !item.icom
    },
    deleteList(item){
      console.log(item);
      let index = this.list.indexOf(item);
      this.list.splice(index,1)
      
      }
    }
};
</script>
<style lang="scss" scope>
.home {
  padding: 0;
  width: 700px;
  height: 700px;
  margin: auto;
  border-radius: 5px;
  box-shadow: 0 0 2px 1px rgb(0, 0, 0, 20%);
  &-container {
    height: 40px;
    background-color: rgb(166, 186, 247);
    text-align: center;
    color: #fff;
  }
  &-body {
    margin: auto;
    text-align: center;
  }
  &-foot {
    width: 100%;
    height: 557px;
    display: flex;
  }
  &-left {
    width: 30%;
    height: 557px;
  }
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  li {
    width: 100%;
    height: 50px;
    text-align: center;
    padding-top: 20px;
  }
  input {
    width: 60%;
    height: 25px;
    border-radius: 5px;
    outline: none;
    text-indent: 1em;
  }
  &-right {
    height: 557px;
    width: 70%;
  }
  &-list {
    height: 50px;
    border-bottom: 1px solid;
    display: flex;
    justify-content: space-between;
    text-align: center;
    align-items: center;
    margin: 30px;
  }
}
.yes {
  width: 30px;
  height: 30px;
  border: solid 1px;
  padding: 0;
  margin: 0;
}
p {
  padding: 0;
  margin: 0;
}
</style>
